package com.test.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.ResData;
import com.test.entity.DepartInfo;
import com.test.entity.JobInfo;
import com.test.entity.StaffInfo;
import com.test.service.DepartInfoService;
import com.test.service.JobInfoService;
import com.test.service.StaffInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 普通员工(StaffInfo)表控制层
 *
 * @author makejava
 * @since 2022-11-12 11:18:51
 */
@Controller
public class AdminStaffInfoController {

    @Autowired
    private StaffInfoService staffInfoService;
    @Autowired
    private DepartInfoService departInfoService;
    @Autowired
    private JobInfoService jobInfoService;

//    显示员工页面
    @GetMapping("admin/staff_info")
    public ModelAndView getStaffInfo(){
        ModelAndView modelAndView = new ModelAndView("admin/staff_info/list");
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        return modelAndView;
    }

//    显示页面员工信息
    @GetMapping("admin/staff_info/queryList")
    @ResponseBody
    public String getQueryList(Integer page,StaffInfo staffInfo) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(staffInfoService.queryByPage(staffInfo,PageRequest.of(page-1,10)));
    }

//    跳转新增页面
    @GetMapping("admin/staff_info/add")
    public ModelAndView getAdd(){
        ModelAndView modelAndView = new ModelAndView("admin/staff_info/add_page");
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        modelAndView.addObject("jobInfoList",jobInfoService.selectAll());
        return modelAndView;
    }
//    提交新增数据
    @PostMapping("admin/staff_info/add_submit")
    @ResponseBody
    public ResData postAddSubmit(StaffInfo staffInfo){
        ResData resData = new ResData();
        int insert = staffInfoService.insert(staffInfo);
        if (insert>0){
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }
//    跳转修改页面
    @GetMapping("admin/staff_info/update")
    public ModelAndView getUpdate(Integer id){
        ModelAndView modelAndView = new ModelAndView("admin/staff_info/update_page");
        modelAndView.addObject("data",staffInfoService.queryById(id));
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        modelAndView.addObject("jobInfoList",jobInfoService.selectAll());
        return modelAndView;
    }
//    提交修改数据
    @PostMapping("admin/staff_info/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(Integer id,StaffInfo staffInfo){
        staffInfo.setId(id);
        ResData resData = new ResData();
        int update = staffInfoService.update(staffInfo);
        if (update>0){
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("修改失败");
        return resData;

    }
//    提交删除数据
    @GetMapping("admin/staff_info/del")
    @ResponseBody
    public ResData getDelete(Integer id){
        ResData resData = new ResData();
        boolean b = staffInfoService.deleteById(id);
        if (b){
            resData.setCode(1);
            resData.setMsg("删除成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("删除失败");
        return resData;
    }

}

